import ajax from 'axios';
import qs from 'qs';
import globalfunc from "../../plugins/globalfunc";
const modules = {
  namespaced:true,
  state:{
    showPlayer:false,
      // sessionStorage['showPlayer']?sessionStorage['showPlayer']:false,
    PlayerList:[],
    currentIndex:0,
    schemaIndex:0,
    MusicDetail:{
      al:{
        name:'',
        picUrl:''
      },ar:[]
    },
lyric:[],
    SongUrl:''
  },
  mutations: {
    AddOneToPlayer(state, info) {
      state.PlayerList.push(info)
      sessionStorage["PlayerList"] = JSON.stringify(state.PlayerList);
      state.showPlayer = true
      state.currentIndex = state.PlayerList.length - 1
    },
    nextMusic(state) {
        if (state.currentIndex >=JSON.parse(sessionStorage['PlayerList']).length - 1) {
          state.currentIndex = 0;
        }
        else {state.currentIndex++;}
      },
    preMusic(state){
      if (state.currentIndex <= 0) {
        state.currentIndex = JSON.parse(sessionStorage['PlayerList']).length - 1;
      }
      else {state.currentIndex--;}
    },
    Schema(state){
      if (state.schemaIndex >= 2) {
        state.schemaIndex = 0;
      }
      else {state.schemaIndex++;}
    },
    MusicDetail(state,info){
state.MusicDetail=info[0]
    },

lyric(state,info){
state.lyric=globalfunc.formatLyric(info)
},
    getSongUrl(state,info){
      state.SongUrl=info
    }

    },
  actions:{
    AddOneToPlayer({commit},playload){
      ajax.get('/api3/song/url?id=' + playload.id).then((res)=>{
        console.log(res.data)
        commit('AddOneToPlayer',{musicinfo:playload,url:res.data.data[0].url});
      }).catch((error)=>{
        console.log(error)
      })

    },
    MusicNext(context) {
        context.commit('nextMusic')
      },
    MusicPrev(context) {
      context.commit('preMusic')
    },
    Schema(context){
      context.commit('Schema')
    },
    MusicDetail({commit},id){//音乐详情
        ajax.get('/api3/song/detail?ids='+id).then((res)=>{
          commit('MusicDetail',res.data.songs);
        }).catch((error)=>{
          console.log(error)
        })
    },
    getLyric({commit},id){//音乐详情
      ajax.get('/api3/lyric?id='+id).then((res)=>{
        commit('lyric',res.data.lrc.lyric);
      }).catch((error)=>{
        console.log(error)
      })
    },
    getSongUrl({commit},id){//音乐url
      ajax.get('/api3/song/url?id='+id).then((res)=>{
        commit('getSongUrl',res.data.data[0].url);
      }).catch((error)=>{
        console.log(error)
      })
    },



  }
}
export default modules
